package com.ruix.app.model;

import android.text.TextUtils;

import com.ruix.app.util.LogUitl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by hadexs on 2017/4/21.
 */

public class Notice implements Serializable{

    private String id;  //id
    private String title; //
    private String content; //
    private String roleID; //
    private String authorid;
    private long ceratedTime; //
    private boolean needConfirm; //
    private boolean confirmed; //是否已经确认
    private boolean readed;//是否已读
    private boolean richtext;//是否是富文本

    private ArrayList album; //图片
    private String confirm;//
    private String displayCreatedTime;
    private int confirmNum;//
    private int memberNum;//
    private ArrayList<Comment> comments; //评论列表


    public Notice parse(JSONObject object){
        if (object != null){
            setId(object.optString("id",""));
            setTitle(object.optString("title",""));
            setContent(object.optString("content",""));
            setConfirmed("1".equals(object.optString("confirm","0")));
            setConfirmNum(object.optInt("confirm_num",0));
            setMemberNum(object.optInt("member_num",0));
            setAuthorid(object.optString("author_id",""));
            setRoleID(object.optString("author_role",""));
            setRichtext(object.optString("richtext","0").equals("1"));
            long creatTime = object.optLong("created_at",0) * 1000;
            setCeratedTime(creatTime);
            setReaded(object.optInt("read",0) == 1);
            setNeedConfirm(object.optInt("need_confirm",0) == 1);

            JSONArray albumArray =  object.optJSONArray("album");
            if (albumArray != null && albumArray.length() > 0) {
                album = new ArrayList();
                for (int i = 0; i < albumArray.length(); i++) {
//                    album.add("http://img.taopic.com/uploads/allimg/120423/107913-12042323220753.jpg");
                    album.add(albumArray.optString(i,"") );
                }
            }
        }
        buildeDisplayTime();

        return this;

    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getRoleID() {
        return roleID;
    }

    public void setRoleID(String roleID) {
        this.roleID = roleID;
    }

    public long getCeratedTime() {
        return ceratedTime;
    }

    public void setCeratedTime(long ceratedTime) {
        this.ceratedTime = ceratedTime;
    }

    public boolean isNeedConfirm() {
        return needConfirm;
    }

    public void setNeedConfirm(boolean needConfirm) {
        this.needConfirm = needConfirm;
    }

    public ArrayList getAlbum() {
        return album;
    }

    public void setAlbum(ArrayList album) {
        this.album = album;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public int getConfirmNum() {
        return confirmNum;
    }

    public void setConfirmNum(int confirmNum) {
        this.confirmNum = confirmNum;
    }

    public int getMemberNum() {
        return memberNum;
    }

    public void setMemberNum(int memberNum) {
        this.memberNum = memberNum;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public void setConfirmed(boolean confirmed) {
        this.confirmed = confirmed;
    }

    public String getDisplayCreatedTime() {
        return displayCreatedTime;
    }

    private void buildeDisplayTime() {
        if (ceratedTime > 0) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            Date d1 = new Date(ceratedTime);
            displayCreatedTime = format.format(d1);
        }
    }

    public ArrayList<Comment> getComments() {
        return comments;
    }

    public void setComments(ArrayList<Comment> comments) {
        this.comments = comments;
    }

    public void addComment(Comment comment){
        if (comments == null){
            comments = new ArrayList();
        }
        comments.add(comment);
    }

    public String getAuthorid() {
        return authorid;
    }

    public void setAuthorid(String authorid) {
        this.authorid = authorid;
    }

    public boolean isReaded() {
        return readed;
    }

    public void setReaded(boolean readed) {
        this.readed = readed;
    }

    public void removeComment(Comment comment){
        if (comments != null){
            for (int i = 0;i< comments.size();i++){
                if (comments.get(i).getID().equals(comment.getID())){
                    comments.remove(i);
                    break;
                }
            }
        }
    }

    public void removeCommentWithiPosition(int positon){
        if (comments != null && comments.size() > positon ){
            comments.remove(positon);
        }
    }

    public boolean isRichtext() {
        return richtext;
    }

    public void setRichtext(boolean richtext) {
        this.richtext = richtext;
    }

    public boolean isShowRoleFrom(){
        return "5".equals(roleID) || "8".equals(roleID);
    }



    public String roleName(){
        if (!TextUtils.isEmpty(roleID)) {
            if (Role.RoleMentor.role().equals(roleID)){
                return Role.RoleMentor.value();
            } else if (Role.RoleSignPics.role().equals(roleID)){
                return Role.RoleSignPics.value();
            } else if (Role.RoleGuest.role().equals(roleID)){
                return Role.RoleGuest.value();
            } else if (Role.RoleAdviser.role().equals(roleID)){
                return Role.RoleAdviser.value();
            } else if (Role.RoleCommittee.role().equals(roleID)){
                return Role.RoleCommittee.value();
            } else if (Role.RoleMonitor.role().equals(roleID)){
                return Role.RoleMonitor.value();
            } else if (Role.RoleSchoolmate.role().equals(roleID)){
                return Role.RoleSchoolmate.value();
            }

        }
        return "";
    }

}
